/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package UML_ER.impl;

import UML_ER.Entity;
import UML_ER.Relationship;
import UML_ER.Role;
import UML_ER.UML_ERPackage;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Role</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link UML_ER.impl.RoleImpl#getConnectedEntity <em>Connected Entity</em>}</li>
 *   <li>{@link UML_ER.impl.RoleImpl#getRelationship <em>Relationship</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class RoleImpl extends MultiplicityElementImpl implements Role {
	/**
	 * The cached value of the '{@link #getConnectedEntity() <em>Connected Entity</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getConnectedEntity()
	 * @generated
	 * @ordered
	 */
	protected Entity connectedEntity;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected RoleImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return UML_ERPackage.Literals.ROLE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Entity getConnectedEntity() {
		if (connectedEntity != null && connectedEntity.eIsProxy()) {
			InternalEObject oldConnectedEntity = (InternalEObject)connectedEntity;
			connectedEntity = (Entity)eResolveProxy(oldConnectedEntity);
			if (connectedEntity != oldConnectedEntity) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, UML_ERPackage.ROLE__CONNECTED_ENTITY, oldConnectedEntity, connectedEntity));
			}
		}
		return connectedEntity;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Entity basicGetConnectedEntity() {
		return connectedEntity;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setConnectedEntity(Entity newConnectedEntity) {
		Entity oldConnectedEntity = connectedEntity;
		connectedEntity = newConnectedEntity;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, UML_ERPackage.ROLE__CONNECTED_ENTITY, oldConnectedEntity, connectedEntity));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Relationship getRelationship() {
		if (eContainerFeatureID() != UML_ERPackage.ROLE__RELATIONSHIP) return null;
		return (Relationship)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetRelationship(Relationship newRelationship, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newRelationship, UML_ERPackage.ROLE__RELATIONSHIP, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setRelationship(Relationship newRelationship) {
		if (newRelationship != eInternalContainer() || (eContainerFeatureID() != UML_ERPackage.ROLE__RELATIONSHIP && newRelationship != null)) {
			if (EcoreUtil.isAncestor(this, newRelationship))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newRelationship != null)
				msgs = ((InternalEObject)newRelationship).eInverseAdd(this, UML_ERPackage.RELATIONSHIP__ROLE, Relationship.class, msgs);
			msgs = basicSetRelationship(newRelationship, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, UML_ERPackage.ROLE__RELATIONSHIP, newRelationship, newRelationship));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case UML_ERPackage.ROLE__RELATIONSHIP:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetRelationship((Relationship)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case UML_ERPackage.ROLE__RELATIONSHIP:
				return basicSetRelationship(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
			case UML_ERPackage.ROLE__RELATIONSHIP:
				return eInternalContainer().eInverseRemove(this, UML_ERPackage.RELATIONSHIP__ROLE, Relationship.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case UML_ERPackage.ROLE__CONNECTED_ENTITY:
				if (resolve) return getConnectedEntity();
				return basicGetConnectedEntity();
			case UML_ERPackage.ROLE__RELATIONSHIP:
				return getRelationship();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case UML_ERPackage.ROLE__CONNECTED_ENTITY:
				setConnectedEntity((Entity)newValue);
				return;
			case UML_ERPackage.ROLE__RELATIONSHIP:
				setRelationship((Relationship)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case UML_ERPackage.ROLE__CONNECTED_ENTITY:
				setConnectedEntity((Entity)null);
				return;
			case UML_ERPackage.ROLE__RELATIONSHIP:
				setRelationship((Relationship)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case UML_ERPackage.ROLE__CONNECTED_ENTITY:
				return connectedEntity != null;
			case UML_ERPackage.ROLE__RELATIONSHIP:
				return getRelationship() != null;
		}
		return super.eIsSet(featureID);
	}

} //RoleImpl
